package com.das.mechanic_base.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.base.X3BaseActivity;

import java.security.MessageDigest;

/**
 * @author chensi
 */
public class X3GlideNewUtils {

    /**
     * 加载矩形图片
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadNormalImage(Context mContext, Object path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder)
                .error(placeHolder)
                .centerCrop();

        Glide.with(mContext)
                .load(path)
                .thumbnail(0.1f)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载矩形图片
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadNormalImage2(Context mContext, Object path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder)
                .error(placeHolder);

        Glide.with(mContext)
                .load(path)
                .thumbnail(0.1f)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载矩形图片
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadNormalFitXyImage(Context mContext, Object path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder);

        Glide.with(mContext)
                .load(path)
                .thumbnail(0.1f)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载矩形图片 不中心裁剪
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadNormalNoCropImage(Context mContext, Object path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }

        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder);

        Glide.with(mContext)
                .load(path)
                .thumbnail(0.1f)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadCircleImage(Context mContext, String path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder)
                .circleCrop();

        Glide.with(mContext)
                .load(path)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆形图片
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadFixCircleImage(Context mContext, String path, ImageView imageView,int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolder)
                .error(placeHolder)
                .circleCrop();

        Glide.with(mContext)
                .load(path)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * @param mContext
     * @param path
     * @param imageView
     * @param radius
     */
    public static void loadRoundCornerImage(Context mContext, String path,
                                            ImageView imageView,int radius,
                                            int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }

        RequestOptions options = new RequestOptions()
                .transforms(new CenterCrop(),new RoundedCorners(radius))
                .placeholder(placeHolder)
                .error(placeHolder);

        Glide.with(mContext)
                .load(path)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * @param mContext
     * @param path
     * @param imageView
     * @param radius
     */
    public static void loadRoundOriginCornerImage(Context mContext, String path,
                                            ImageView imageView,int radius,
                                            int placeHolder) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .transform(new RoundedCorners(radius))
                .placeholder(placeHolder);

        Glide.with(mContext)
                .load(path)
                .apply(options)
                .into(imageView);
    }

    /**
     * 加载圆形带边框的图片
     * @param mContext
     * @param path
     * @param imageView
     * @param borderColor
     * @param borderWidth
     */
    public static void loadCircleBorderImage(Context mContext, String path, ImageView imageView,int borderColor,int borderWidth) {
        if (mContext == null) {
            return;
        }
        if (mContext instanceof X3BaseActivity) {
            if (((X3BaseActivity) mContext).isDestroyed()) {
                return;
            }
        }
        RequestOptions options = new RequestOptions()
                .placeholder(Color.BLACK)
                .transform(new GlideCircleWithBorder(mContext,borderWidth,borderColor))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true);

        Glide.with(mContext)
                .load(path)
                .apply(options)
                .into(imageView);
    }

    static class GlideCircleWithBorder extends BitmapTransformation {

        private Paint mBorderPaint;
        private float mBorderWidth;

        public GlideCircleWithBorder(Context context) {
            super();
        }

        public GlideCircleWithBorder(Context context, int borderWidth, int borderColor) {
            super();
            mBorderWidth = borderWidth;

            mBorderPaint = new Paint();
            mBorderPaint.setDither(true);
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setColor(borderColor);
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setStrokeWidth(mBorderWidth);
        }

        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            return circleCrop(pool, toTransform);
        }

        private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
            if (source == null) {
                return null;
            }
            int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
            Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
            Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            }
            //创建画笔 画布 手动描绘边框
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            float r = size / 2f;
            canvas.drawCircle(r, r, r, paint);
            if (mBorderPaint != null) {
                float borderRadius = r - mBorderWidth / 2;
                canvas.drawCircle(r, r, borderRadius, mBorderPaint);
            }
            return result;
        }

        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {

        }
    }
}
